from config.settings import Settings
from sqlalchemy.ext.asyncio import AsyncAttrs, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase

database_url = Settings().database_url_v1

async_engine = create_async_engine(
    database_url,
)
AsyncSessionLocal = async_sessionmaker(
    autocommit=False, autoflush=False, bind=async_engine
)


async def get_db(type: str = "v1"):
    if type == "v1":
        database_url = Settings().database_url_v1
    else:
        database_url = Settings().database_url_v2

    async_engine = create_async_engine(
        database_url,
    )
    AsyncSessionLocal = async_sessionmaker(
        autocommit=False, autoflush=False, bind=async_engine
    )
    async with AsyncSessionLocal() as db:
        yield db


class Base(AsyncAttrs, DeclarativeBase):
    pass
